﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyHealthOnline.Dal.Generated.CollectionClasses;
using MyHealthOnline.Business;
using MyHealthOnline.Dal.Generated.EntityClasses;

namespace MyHealthOnline.Patient
{
    public partial class ViewConsult : System.Web.UI.Page
    {
        public int ConsultId
        {
            get
            {
                return Convert.ToInt32(Request.QueryString["ConsultId"]);
            }
        }

        public ConsultEntity Consult
        {
            get
            {
                return ConsultBusiness.GetConsult(ConsultId);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                SetupQuestionList();
                SetupQuestionnaireList();
                SetupMessageHistory();
                ucEnd.ConsultId = ConsultId;
                ucAppointments.ConsultId = ConsultId;
            }

        }

        private void SetupMessageHistory()
        {
            ucMessageHistory.SetDataSource( QuestionBusiness.GetAnsweredQuestionsByConsult(ConsultId)); //MessageBusiness.GetMessages(ConsultId);
        }

        private void SetupQuestionList()
        {
            ucQuestions.DataSource = QuestionBusiness.GetUnansweredQuestionsByConsult(ConsultId);
        }

        private void SetupQuestionnaireList()
        {
            rblQuestionnaires.DataSource = QuestionnaireBusiness.GetUnansweredQuestionnairesByConsult(ConsultId); 
            rblQuestionnaires.DataBind();

            if (rblQuestionnaires.Items.Count == 0)
            {
                rblQuestionnaires.Visible = false;
                lblNoQuestionnaires.Visible = true;
            }
            else
            {
                lblNoQuestionnaires.Visible = false;
                rblQuestionnaires.Visible = true;
            }
        }

        protected void btnOpenQuestionnaire_Click(object sender, EventArgs e)
        {
            int questionnaireId = Convert.ToInt32(rblQuestionnaires.SelectedValue);
            Response.Redirect(
                string.Format("~/Patient/OpenQuestionnaire.aspx?QuestionnaireId={0}&ConsultId={1}",
                    questionnaireId,ConsultId));
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Patient/ViewPatientConsultations.aspx");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ucQuestions.SaveAnswers();

            WebServiceHO webService = new WebServiceHO();
            webService.Notify(Consult.Pacient.Email, Consult.Specialist.Email,
                "Question answered by patient " + Consult.PacientName,
                "Question answered");

            Response.Redirect("~/Patient/ViewPatientConsultations.aspx");
        }
    }
}